package net.plankton.context;

import net.plankton.agents.Agent;
import net.plankton.context.occurrence.Occurrence;
import net.plankton.context.threat.NoThreat;
import net.plankton.context.threat.ThreatLevel;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class RiskContext {

	public RiskContext(long time, String place) {
		this.time = time;
		this.place = place;
	}
	
	private long time;
	private String place;
	private List<Agent> involvedAgents = new ArrayList<Agent>();
	private ThreatLevel threat = new NoThreat();
	private List<Consequence> consequences=new ArrayList<Consequence>();
	private List<Occurrence> occurrences = new ArrayList<Occurrence>();
	
	
	
	
	private double threatLevel = 0.1;
	
	public String reportActualRisk() {
		StringBuilder builder = new StringBuilder();
		builder.append("Reporting actual risks:\n")
		.append("current time: ")
		.append(new Date(time).toGMTString())
		.append("\n")
		.append(place)
		.append("\n");
		
		builder.append("\n The actual threat level is ")
		.append(threat.getClass().getName())
		.append("\n")
		.append("->")
		.append(threat.describe())
		.append("\n");
		
		
		if (involvedAgents.size()>0) {
			builder.append("\ncurrently involved agents:\n");
			for (Iterator<Agent> it = involvedAgents.iterator();it.hasNext();) {
				builder.append(it.next().getClass().getName())
				.append("\n");
			}
		}
		else {
			builder.append("\ncurrently no agents involved\n");
		}
		builder.append("\n");
		
		if (occurrences.size()>0) {
			builder.append("\noccurrences till now:\n");
			for (Iterator<Occurrence> it = occurrences.iterator();it.hasNext();) {
				Occurrence o = it.next();
				builder.append(o.getClass().getName())
				.append(" : "+o.describe())
				.append("\n");
			}
		}
		else {
			builder.append("\nno occurrences\n");
		}
		builder.append("\n");
		
		if (consequences.size()>0) {
			builder.append("\nconsequences till now:\n");
			for (Iterator<Consequence> it = consequences.iterator();it.hasNext();) {
				Consequence o = it.next();
				builder.append(o.getClass().getName())
				.append(" : "+o.describe())
				.append("\n");
			}
		}
		else {
			builder.append("\nno consequences\n");
		}
		builder.append("\n");
		
		return builder.toString();
	}



	public void changeThreatLevel(double t) {
		
		threatLevel = (threatLevel+t)-(threatLevel*t);
//		System.err.println("ThreatLevel changed: "+threatLevel);
	}



	public double getThreatLevel() {
		
		return threatLevel;
	}
	
}
